package com.byb.server.service.impl;

import com.byb.server.service.OtherSafeguard2ndService;
import com.byb.server.domain.OtherSafeguard2nd;
import com.byb.server.repository.OtherSafeguard2ndRepository;
import com.byb.server.repository.search.OtherSafeguard2ndSearchRepository;
import com.byb.server.service.dto.OtherSafeguard2ndDTO;
import com.byb.server.service.mapper.OtherSafeguard2ndMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing OtherSafeguard2nd.
 */
@Service
@Transactional
public class OtherSafeguard2ndServiceImpl implements OtherSafeguard2ndService{

    private final Logger log = LoggerFactory.getLogger(OtherSafeguard2ndServiceImpl.class);

    private final OtherSafeguard2ndRepository otherSafeguard2ndRepository;

    private final OtherSafeguard2ndMapper otherSafeguard2ndMapper;

    private final OtherSafeguard2ndSearchRepository otherSafeguard2ndSearchRepository;
    public OtherSafeguard2ndServiceImpl(OtherSafeguard2ndRepository otherSafeguard2ndRepository, OtherSafeguard2ndMapper otherSafeguard2ndMapper, OtherSafeguard2ndSearchRepository otherSafeguard2ndSearchRepository) {
        this.otherSafeguard2ndRepository = otherSafeguard2ndRepository;
        this.otherSafeguard2ndMapper = otherSafeguard2ndMapper;
        this.otherSafeguard2ndSearchRepository = otherSafeguard2ndSearchRepository;
    }

    /**
     * Save a otherSafeguard2nd.
     *
     * @param otherSafeguard2ndDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public OtherSafeguard2ndDTO save(OtherSafeguard2ndDTO otherSafeguard2ndDTO) {
        log.debug("Request to save OtherSafeguard2nd : {}", otherSafeguard2ndDTO);
        OtherSafeguard2nd otherSafeguard2nd = otherSafeguard2ndMapper.toEntity(otherSafeguard2ndDTO);
        otherSafeguard2nd = otherSafeguard2ndRepository.save(otherSafeguard2nd);
        OtherSafeguard2ndDTO result = otherSafeguard2ndMapper.toDto(otherSafeguard2nd);
        //otherSafeguard2ndSearchRepository.save(otherSafeguard2nd);
        return result;
    }

    /**
     *  Get all the otherSafeguard2nds.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OtherSafeguard2ndDTO> findAll(Pageable pageable) {
        log.debug("Request to get all OtherSafeguard2nds");
        return otherSafeguard2ndRepository.findAll(pageable)
            .map(otherSafeguard2ndMapper::toDto);
    }

    /**
     *  Get one otherSafeguard2nd by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public OtherSafeguard2ndDTO findOne(Long id) {
        log.debug("Request to get OtherSafeguard2nd : {}", id);
        OtherSafeguard2nd otherSafeguard2nd = otherSafeguard2ndRepository.findOne(id);
        return otherSafeguard2ndMapper.toDto(otherSafeguard2nd);
    }

    /**
     *  Delete the  otherSafeguard2nd by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete OtherSafeguard2nd : {}", id);
        otherSafeguard2ndRepository.delete(id);
        //otherSafeguard2ndSearchRepository.delete(id);
    }

    /**
     * Search for the otherSafeguard2nd corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<OtherSafeguard2ndDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of OtherSafeguard2nds for query {}", query);
        Page<OtherSafeguard2nd> result = otherSafeguard2ndSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(otherSafeguard2ndMapper::toDto);
    }

    @Override
    public Page<OtherSafeguard2ndDTO> getAllProductId(Long id, Pageable pageable) {
        return  otherSafeguard2ndRepository.findAllByProductId(id,pageable).map(otherSafeguard2ndMapper::toDto);
    }
}
